package com.iedu.demo.lgq.controller;

import com.iedu.demo.lgq.entity.TbOrderReview;
import com.iedu.demo.lgq.entity.TbReviewMedia;
import com.iedu.demo.lgq.service.OrderReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/reviews")
public class OrderReviewController {

    @Autowired
    private OrderReviewService orderReviewService;

    // 添加评价
//    @PostMapping("/add")
//    public TbOrderReview addReview(@RequestBody TbOrderReview review) {
//        return orderReviewService.addReview(review, null);
//    }

    @PostMapping("/add")
    public TbOrderReview addReview(
            @RequestParam(value = "item_id", required = false) Integer itemId,   // 解析评论的 item_id
            @RequestParam(value = "user_id",required = false) Integer userId,   // 解析评论的 user_id
            @RequestParam(value = "content", required = false) String content, // 解析评论的内容
            @RequestParam(value = "files", required = false) MultipartFile[] files) {  // 解析上传的文件数组
        // 调用服务层的逻辑来保存评论
        List<TbReviewMedia> reviewMediaList = new ArrayList<>();
        if (files != null && files.length > 0) {
            for (MultipartFile file : files) {
                TbReviewMedia reviewMedia = new TbReviewMedia();
                String fileName = file.getOriginalFilename();
                String fileSuffixName = fileName.split("\\.")[1];
                String imagePath = "C:\\Users\\86156\\Desktop\\bt\\lgqui\\public\\images";
                fileName = UUID.randomUUID().toString().replace("-", "") + "." + fileSuffixName;
                try {
                    file.transferTo(new File("C:\\Users\\86156\\Desktop\\bt\\lgqui\\public\\images", fileName));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                reviewMedia.setCommentId(itemId);
                reviewMedia.setFileType(fileSuffixName);
                reviewMedia.setFileUrl(imagePath.split("public")[1] + "\\" + fileName);
                reviewMediaList.add(reviewMedia);
            }
        }
        if (reviewMediaList.size() > 0) {
            TbOrderReview review = new TbOrderReview(null,itemId, userId,content,null,reviewMediaList);
            return orderReviewService.addReview(review,reviewMediaList);
        }
        TbOrderReview review = new TbOrderReview(null,itemId, userId,content,null,null);
        return orderReviewService.addReview(review,null);
    }

    // 根据商品 ID 获取评价
    @GetMapping("/item/{itemId}")
    public List<TbOrderReview> getReviewsByItemId(@PathVariable Integer itemId) { // 修改为 itemId
        return orderReviewService.getReviewsByItemId(itemId);
    }

    // 根据用户 ID 获取评价
    @GetMapping("/user/{userId}")
    public List<TbOrderReview> getReviewsByUserId(@PathVariable Integer userId) {
        return orderReviewService.getReviewsByUserId(userId);
    }

    // 根据评价 ID 获取评价
    @GetMapping("/{reviewId}")
    public TbOrderReview getReviewById(@PathVariable Integer reviewId) {
        return orderReviewService.getReviewById(reviewId);
    }

    // 更新评价
    @PutMapping
    public TbOrderReview updateReview(@RequestBody TbOrderReview review) {
        return orderReviewService.updateReview(review);
    }

    // 删除评价
    @DeleteMapping("/{reviewId}")
    public void deleteReview(@PathVariable Integer reviewId) {
        orderReviewService.deleteReview(reviewId);
    }
}
